const app = getApp()

Page({
  data: {
    team: null,
    activity: null,
    countdown: '00:00:00',
    timer: null
  },

  onLoad() {
    const userInfo = wx.getStorageSync('userInfo')
    if (!userInfo || userInfo.role !== 'member') {
      wx.redirectTo({
        url: '/pages/login/login'
      })
      return
    }

    this.loadTeamInfo(userInfo.teamId)
    this.loadActivityInfo(userInfo.activityId)
  },

  onShow() {
    // 每次显示页面时重新加载数据
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.loadTeamInfo(userInfo.teamId)
      this.loadActivityInfo(userInfo.activityId)
    }
  },

  onHide() {
    // 清除定时器
    if (this.data.timer) {
      clearInterval(this.data.timer)
    }
  },

  onUnload() {
    // 清除定时器
    if (this.data.timer) {
      clearInterval(this.data.timer)
    }
  },

  // 加载队伍信息
  loadTeamInfo(teamId) {
    wx.cloud.callFunction({
      name: 'getTeamInfo',
      data: { teamId }
    }).then(res => {
      this.setData({
        team: res.result.data
      })
    }).catch(err => {
      console.error('获取队伍信息失败：', err)
      wx.showToast({
        title: '获取队伍信息失败',
        icon: 'none'
      })
    })
  },

  // 加载活动信息
  loadActivityInfo(activityId) {
    wx.cloud.callFunction({
      name: 'getActivityDetail',
      data: { id: activityId }
    }).then(res => {
      const activity = res.result.data
      this.setData({ activity })

      // 如果活动正在进行，启动倒计时
      if (activity.status === 'ongoing') {
        this.startCountdown(activity.startTime, activity.endTime)
      }
    }).catch(err => {
      console.error('获取活动信息失败：', err)
      wx.showToast({
        title: '获取活动信息失败',
        icon: 'none'
      })
    })
  },

  // 启动倒计时
  startCountdown(startTime, endTime) {
    // 清除之前的定时器
    if (this.data.timer) {
      clearInterval(this.data.timer)
    }

    const updateCountdown = () => {
      const now = new Date()
      const [startHour, startMinute] = startTime.split(':')
      const [endHour, endMinute] = endTime.split(':')

      const startDate = new Date(now)
      startDate.setHours(startHour, startMinute, 0)

      const endDate = new Date(now)
      endDate.setHours(endHour, endMinute, 0)

      // 如果当前时间在活动时间范围内
      if (now >= startDate && now <= endDate) {
        const diff = endDate - now
        const hours = Math.floor(diff / (1000 * 60 * 60))
        const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
        const seconds = Math.floor((diff % (1000 * 60)) / 1000)

        this.setData({
          countdown: `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
        })
      } else {
        // 如果不在活动时间范围内，显示00:00:00
        this.setData({
          countdown: '00:00:00'
        })
        // 清除定时器
        clearInterval(this.data.timer)
      }
    }

    // 立即执行一次
    updateCountdown()

    // 设置定时器，每秒更新一次
    const timer = setInterval(updateCountdown, 1000)
    this.setData({ timer })
  },

  // 跳转到任务列表
  goToTaskList() {
    wx.navigateTo({
      url: '/pages/member/taskList/taskList'
    })
  },

  // 跳转到排名页面
  goToRanking() {
    wx.navigateTo({
      url: '/pages/member/ranking/ranking'
    })
  }
}) 